<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Strings</em>
</p>
<p class='vspace'></p><h3>String IndexOf() and lastIndexOf() Methods</h3>
<p>The String object indexOf() method gives you the ability to search for the first instance of a particular character value in a String. You can also look for the first instance of the character after a given offset. The lastIndexOf() method lets you do the same things from the end of a String.
</p>
<h3>Relevant Groundwork</h3>
<p class='vspace'></p>
<pre>
  String stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int firstClosingBracket = stringOne.indexOf('>');
</pre><p class='vspace'></p>
<p>In this case, firstClosingBracket equals 5, because the first > character is at position 5 in the String (counting the first character as 0). If you want to get the second closing bracket, you can use the fact that you know the position of the first one, and search from firstClosingBracket + 1 as the offset, like so:
</p><p class='vspace'></p>
<pre>
  stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int secondClosingBracket = stringOne.indexOf('>', firstClosingBracket + 1 );
</pre><p class='vspace'></p>
<p>The result would be 11, the position of the closing bracket for the HEAD tag.
</p><p class='vspace'></p>
<p>If you want to search from the end of the String, you can use the lastIndexOf() method instead. This function returns the position of the last occurrence of a given character.
</p><p class='vspace'></p>
<pre>
  stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int lastOpeningBracket = stringOne.lastIndexOf('<');
</pre><p class='vspace'></p>
<p>In this case, lastOpeningBracket equals 12, the position of the < for the BODY tag. If you want the opening bracket for the HEAD tag, it would be at stringOne.lastIndexOf('<', lastOpeningBracket -1), or 6.
</p>
<h3>Hardware Required</h3>
<ul>
<li>MSP-EXP430G2 LaunchPad</li>
</ul>
<h3>Circuit</h3>
<p>Only LaunchPad is required for this example.
</p>
<img height='250px' src='../reference/img/Blink_bb.png' alt='' title='' />
<p>image developed using <a href='http://fritzing.org'>Fritzing</a>. For more circuit examples, see the <a href='http://fritzing.org/projects'>Fritzing project page</a>.
</p>
<h3>Schematic</h3>
<img height='400px' src='../reference/img/Blink_schem.png' alt='' title='' />
<h3>Code Explanation</h3>
<p>In the program, we use string index to look for and replace characters in a string.
</p>
<h3>Code</h3>
<pre>
/*
  String indexOf() and lastIndexOf() functions
 
 Examples of how to evaluate, look for, and replace characters in a String
 
 created 27 July 2010
 modified 2 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain. 
 */

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // send an intro:
  Serial.println("\n\nString indexOf() and lastIndexOf()  functions:");
  Serial.println();
}

void loop() {
  // indexOf() returns the position (i.e. index) of a particular character
  // in a string. For example, if you were parsing HTML tags, you could use it:
  String stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int firstClosingBracket = stringOne.indexOf('>');
  Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);

  stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int secondOpeningBracket = firstClosingBracket + 1;
  int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
  Serial.println("The index of  the second > in the string " + stringOne + " is " + secondClosingBracket);

  // you can also use indexOf() to search for Strings:
  stringOne = "&ltHTML&gt&ltHEAD&gt&ltBODY&gt";
  int bodyTag = stringOne.indexOf("&ltBODY&gt");
  Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);

  stringOne = "&ltUL&gt&ltLI&gtitem&ltLI&gtitem&ltLI&gtitem&lt/UL&gt";
  int firstListItem = stringOne.indexOf("&ltLI&gt");
  int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
  Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);

  // lastIndexOf() gives you the last occurrence of a character or string:
  int lastOpeningBracket = stringOne.lastIndexOf('<');
  Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);

  int lastListItem  = stringOne.lastIndexOf("&ltLI&gt");
  Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);


  // lastIndexOf() can also search for a string:
  stringOne = "&ltp&gtLorem ipsum dolor sit amet&lt/p&gt&ltp&gtIpsem&lt/p&gt&ltp&gtQuod&lt/p&gt";
  int lastParagraph = stringOne.lastIndexOf("&ltp");
  int secondLastGraf = stringOne.lastIndexOf("&ltp", lastParagraph - 1);
  Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);

  // do nothing while true:
  while(true);
}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
<p>- Turn a present tense sentance into past tense using index functions
</p>
<p class='vspace'></p><p><strong>See Also</strong>
</p><ul><li><a class='wikilink' href='Setup.html'>setup</a>()
</li><li><a class='wikilink' href='Loop.html'>loop</a>()
</li><li><a class='wikilink' href='Serial_Println.html'>println</a>()
</li></ul>

<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
